package cn.kgc.novel.controller;

import cn.kgc.novel.common.ErrorCodeEnum;
import cn.kgc.novel.common.Result;
import cn.kgc.novel.pojo.Users;
import cn.kgc.novel.service.UsersService;
import cn.kgc.novel.utils.SendEmail;
import cn.kgc.novel.utils.SmsUtils;
import org.dromara.sms4j.comm.utils.SmsUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
public class SmsController {

    @Resource
    private SendEmail sendEmail;
    @Resource
    private UsersService usersService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/sendSms")
    public Result sendSms(String phone) {
        String code = SmsUtil.getRandomInt(4);
        stringRedisTemplate.opsForValue().set("yanzhengma",code,60, TimeUnit.SECONDS);
        SmsUtils.sendSms(phone, code);
        return Result.ok(code);
    }

    @GetMapping("/sendEmail")
    public Result sendEmail(String email,String subject,String text) {
        sendEmail.sendSimpleEmail(email,subject,text);
        return Result.ok();
    }
}
